﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    [DefaultProperty("ImageUrl")]
    [ToolboxData("<{0}:ImageLink runat=\"server\" NavigateUrl=\"\" ImageUrl=\"\" AlternateText=\"\" />")]
    public class ImageLink : Image
    {
        [Bindable(true)]
        [Category("Navigation")]
        [DefaultValue("#")]
        public string NavigateUrl { get; set; }

        [Bindable(true)]
        [Category("Navigation")]
        [DefaultValue("_blank")]
        public string Target { get; set; }

        private string contentBase = "<a href=\"{0}\" target=\"{5}\"><img title=\"{2}\" src=\"{1}\" alt=\"{2}\" style=\"border-width: 0px;\" /></a>";
        private string contentBaseWidth = "<a href=\"{0}\" target=\"{5}\"><img title=\"{2}\" src=\"{1}\" alt=\"{2}\" style=\"border-width: 0px;width: {3};\" /></a>";
        private string contentBaseHeight = "<a href=\"{0}\" target=\"{5}\"><img title=\"{2}\" src=\"{1}\" alt=\"{2}\" style=\"border-width: 0px;height: {4};\" /></a>";
        private string contentBaseWidthHeight = "<a href=\"{0}\" target=\"{5}\"><img title=\"{2}\" src=\"{1}\" alt=\"{2}\" style=\"border-width: 0px;width: {3};height: {4};\" /></a>";

        protected override void Render(HtmlTextWriter output)
        {
            String outputString = String.Empty;

            if (Width.Value == 0 & Height.Value == 0)
                outputString = contentBase;
            else if (Width.Value > 0 & Height.Value == 0)
                outputString = contentBaseWidth;
            else if (Width.Value == 0 & Height.Value > 0)
                outputString = contentBaseHeight;
            else if (Width.Value > 0 & Height.Value > 0)
                outputString = contentBaseWidthHeight;

            if (this.Target == null)
                this.Target = "_blank";

            output.Write(string.Format(outputString, this.NavigateUrl, this.ImageUrl, this.AlternateText.TextToHtml(), this.Width, this.Height,this.Target));
        }
    }
}
